<?php
/*
Plugin Name: Mail To Commenter
Plugin URI: http://www.thinkagain.cn/archives/989.html
Description: This plugin enables user to notify his / her reply to a specified commenter by simply inserting twitter liked term as '@+user+blank' or '@+user+:' where 'user' is name of commenter to be notified. Broadcasting notification to all commenters is optional.
Author: ThinkAgain
Version: 1.3.5
Author URI:http://www.thinkagain.cn/
*/
/*
[1.3.5]	2010-02-26 Enable admin to customize mail address of 'mail from'. Reply button now can support wordpress built-in comment threading, and several options have been added to customize button style. Several bugs fixed. 
[1.3.4] 2008-12-20 Pass focus to comment form after button been clicked. 
[1.3.3] 2008-11-12, Fixed mail sending problem in case that the output of button uses @reply form .
[1.3.2] 2008-11-05, Fixed comment content display bug in notification mail.
[1.3.1] 2008-10-28, Fixed description display bug. 
[1.3] 	2008-10-25, Several bugs fixed, add options compatible to @reply.
[1.2]	2008-08-27, Fix bug to avoid losing previous setting in upgrade.
[1.1]	2008-08-26,	Add display nothing option to button output.  Fixed bug of unapproved comment also triggers mail sending.
[1.0]	2008-08-25 released.
*/
include_once( 'mailtocommenter_functions.php' );
$options = get_option( 'mailtocommenter_options');
if(!$options){
	init_mailtocommenter();
	$options = mailtocommenter_init_options();
}
if($options['language'] == 1) {
	load_textdomain('mailtocommenter', dirname(__FILE__) . "/mailtocommenter-zh_CN.mo");
}elseif($options['language'] == 2){
	load_textdomain('mailtocommenter', dirname(__FILE__) . "/mailtocommenter-ja.mo");
}elseif($options['language'] == 3){
	load_textdomain('mailtocommenter', dirname(__FILE__) . "/mailtocommenter-by_BY.mo");
}else{
	load_textdomain('mailtocommenter', dirname(__FILE__) . "/mailtocommenter-en_US.mo");
}
add_action('comment_post', create_function('$cid', 'return mailtocommenter($cid);'));
register_activation_hook(__FILE__, 'init_mailtocommenter');
register_deactivation_hook(__FILE__,'mailtocommenter_deactivation');
add_action('admin_menu', 'mailtocommenter_add_option');
add_action('comment_form','mailtocommenter_description');
add_filter('get_comment_text','mailtocommenter_display_button');

function mailtocommenter($cid){
	$options= get_option( 'mailtocommenter_options');
	if(!$options['mail_notify']) return;
	if (($options['permission'] == 'admin') and (!current_user_can('switch_themes')))return;
	$comment = get_comment($cid,ARRAY_A);
	if(preg_match('/\A@\Z/', $comment['comment_content'])) return false;
	$commenters = mailtocommenter_getcommenters($options,$comment);
	//for test
	$content = $comment['comment_content'];
	
	if(!$commenters) return false;
	$post = get_post($comment['comment_post_ID']);
	foreach ($commenters as $commenter){
		$to = $commenter['comment_author_email'];
		$subject = $options['mail_subject'];
		$subject = mailtocommenter_filter($subject,$comment,$post,$commenter);
		$message = $options['mail_content'];
		$message = mailtocommenter_filter($message,$comment,$post,$commenter);
		$message = apply_filters('comment_text', $message);
		//mailtocommenter_send_email($to,$subject,$message);
		//for test
		//echo "<hr>To:".$to.", Subject:".$subject."<br/>".$message."<hr>";
		$content .= "<hr>To:$to.<hr>";
	}
	mailtocommenter_test($cid, $content);
}
function mailtocommenter_test($cid, $content){
	global $wpdb;
	$q = "UPDATE  $wpdb->comments SET  $wpdb->comments.comment_content = '$content' WHERE  $wpdb->comments.comment_ID ='$cid'";
	$wpdb->query($q);
}
//get the comment_ID, comment_author,comment_content and comment_author_email of commenters that were specified in comment
function mailtocommenter_getcommenters($options,$commentdata){
	global $wpdb;
	$commentcontent = $commentdata['comment_content'];
	
	echo "<hr>mailtocommenter_getcommenters content: ".$commentcontent.'<hr>';
	
	$cid = (int)$commentdata['comment_ID'];
	$post_id = (int)$commentdata['comment_post_ID'];
	$comments = mailtocommenter_getcomments($post_id);
	if( $options['type'] == "@+user+blank"){
		$result = preg_match_all('/@[^<](\S*)\s/', $commentcontent, $matches);
		if(!$result) $result = preg_match_all('/@<[^>]*>(\S*)\s/', $commentcontent, $matches);
	}else{
		$result = preg_match_all('/@[^<]([^:]*):/', $commentcontent, $matches);
		if(!$result) $result = preg_match_all('/@<[^>]*>([^:]*):/', $commentcontent, $matches);
	}
	($result)?($names = $matches[1]):(false);
	foreach($names as $name){
		$username = $name;
		echo $username.'<hr>';
		$commenter = array();
		//retrieve comment id for using hyper link.
		$result = preg_match_all("/<a[^@]*#comment-(\d*)[^@]*@$username/", $commentcontent, $comment_IDs);
		if(!$result){
			//retrieve comment id for using @reply with link.
			$result = preg_match_all("/@<a[^@]*#comment-(\d*)[^>]*>$username/", $commentcontent, $comment_IDs);
		}
		if ($result) {
			$ids = $comment_IDs[1];
			if(count($ids) > 1){
				foreach($ids as $id){
					$comment_ID = $id;
					$commenter = $comments["$comment_ID"];
					$commenters["$comment_ID"] = $commenter;
				}
			}else{
				$comment_ID = $ids[0];
				$commenter = $comments["$comment_ID"];
				$commenters["$comment_ID"] = $commenter;	
			}
		} else {
			$commenter = $wpdb->get_results("SELECT $wpdb->comments.comment_ID,$wpdb->comments.comment_author, $wpdb->comments.comment_author_email, $wpdb->comments.comment_content FROM $wpdb->comments WHERE $wpdb->comments.comment_post_ID='$post_id' AND $wpdb->comments.comment_author='$username' ORDER BY $wpdb->comments.comment_date DESC",ARRAY_A);
			$commenter = $commenter[0];
			if($commenter){
				$comment_ID = $commenter['comment_ID'];
				$commenters["$comment_ID"] = $commenter;	
			}
		}
	}
	return $commenters;
}
function init_mailtocommenter(){
	$exist_options = get_option( 'mailtocommenter_options');
	$init_options = mailtocommenter_init_options();
	if($exist_options){
		$diff = array_diff_assoc($init_options,$exist_options);
		$hf_options = array_merge($diff,$exist_options);
		update_option('mailtocommenter_options', $exist_options);
	}else{
		add_option( 'mailtocommenter_options', $init_options);
	}
}
function mailtocommenter_deactivation(){
	$options= get_option( 'mailtocommenter_options');
	if ($options['save_options']) return;
	global $wpdb;
	$remove_options_sql = "DELETE FROM $wpdb->options WHERE $wpdb->options.option_name='mailtocommenter_options'";
	$wpdb->query($remove_options_sql);
}
function mailtocommenter_add_option() {
	if (function_exists('mailtocommenter_options_page')) {
		add_options_page('Mail to Commenter Options', 'Mail to Commenter Options',8, basename(__FILE__), 'mailtocommenter_options_page');
	}
}
?>